package com.xs.ai.controller;

import com.xs.ai.entity.DailyBriefing;
import com.xs.ai.services.DailyBriefingService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api/v1")
@RequiredArgsConstructor
public class DashboardController {

    private final DailyBriefingService briefingService;

    /**
     * 获取今日简报
     */
    @GetMapping("/dashboard/briefing/today")
    public ResponseEntity<DailyBriefing> getTodayBriefing(@RequestParam Long userId) {
        LocalDate today = LocalDate.now();
        DailyBriefing briefing = briefingRepository
                .findByUserIdAndBriefingDate(userId, today)
                .orElseGet(() -> briefingService.generateBriefingForUser(
                        userRepository.findById(userId).orElseThrow()
                ));

        return ResponseEntity.ok(briefing);
    }

    /**
     * 获取工作统计
     */
    @GetMapping("/dashboard/stats")
    public ResponseEntity<Map<String, Object>> getStats(
            @RequestParam Long userId,
            @RequestParam(required = false) String period) {

        Map<String, Object> stats = new HashMap<>();
        stats.put("unreadEmails", emailRepository.countUnreadByUser(userId));
        stats.put("todayMeetings", calendarRepository.countTodayMeetingsByUser(userId));
        stats.put("pendingTasks", taskRepository.countPendingByUser(userId));
        stats.put("weeklyMeetingHours", calendarRepository.getWeeklyMeetingHours(userId));
        stats.put("aiAccuracyRate", userDecisionService.calculateMatchRate(userId));

        return ResponseEntity.ok(stats);
    }
}

